package algo.LocalSearch;

import algo.Model.*;

public class FuncVarConst extends BasicLocalSearchEntity implements IFunction {
	//LocalSolver _ls;
	VarInt	_x = null;
	int _value;
	VarInt[]	_vars;
	BasicLocalSearchEntity[] _e;
	
	public FuncVarConst(VarInt x){
		super(x.getLocalSolver());
		_x = x;
		//_ls = _x.getLocalSolver();
		_value = _x.getValue();
		post();
	}
	public FuncVarConst(LocalSolver ls, int v){
		//_ls = ls;
		super(ls);
		_value = v;
		post();
	}
	//public LocalSolver getLocalSolver(){
		//return _ls;
	//}
	void post(){
		if(_x == null){
			_vars = null;
			_e = null;
		}else{
			_vars = new VarInt[1];
			_vars[0] = _x;
			_e = new BasicLocalSearchEntity[1];
			_e[0] = _x;
		}
		LocalSolver _ls = getLocalSolver();
		if(_ls == null)
			System.out.println("FuncVarConst::post, ls is null");
		_ls.post(this);
	}
	public BasicLocalSearchEntity[] getDependEntities(){
		return _e;
	}
	public String name(){
		return "FuncVarConst[" + getID() + "]";
	}
	public VarInt[] getVariables(){ return _vars;}
	
	@Override
	public int getValue() {
		// TODO Auto-generated method stub
		return _value;
	}

	@Override
	public int getAssignDelta(VarInt x, int val) {
		// TODO Auto-generated method stub
		if(x != _x)
			return 0;
		if(_x == null)
			return 0;
		return val - _x.getValue();
	}

	@Override
	public void propagateInt(VarInt x, int val) {
		// TODO Auto-generated method stub
		if(_x != x)
			return;
		if(_x == null)
			return;
		//_x.setValue(val);
		_value = _x.getValue();// value of _x has already been updated;
		//System.out.println("FuncVarConst::propagate(" + x.name() + "," + val + ") -> value = " + getValue());
	}

	@Override
	public void initPropagate() {
		// TODO Auto-generated method stub
		//System.out.println("FuncVarConst::initPropagate");
		if(_x != null)
			_value = _x.getValue();
		//System.out.println("FuncVarConst::initPropagate, value = " + _value);
	}

	@Override
	public boolean verify() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
